package library.and.xps.com.step.view.activity;

import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jude.utils.JUtils;
import com.squareup.okhttp.Request;
import com.zhy.autolayout.AutoLinearLayout;

import library.and.xps.com.step.R;
import library.and.xps.com.step.URLConstants;
import library.and.xps.com.step.m.model.RequestParams;
import library.and.xps.com.step.m.netapi.OkHttpClientUtils;
import library.and.xps.com.step.net.ResponceWrapper;
import library.and.xps.com.step.util.CommonUtils;
import library.and.xps.com.step.util.RequestCallback;
import library.and.xps.com.step.view.base.BaseActivity;

public class RegisterActivity extends BaseActivity implements View.OnClickListener {
    TextView tvCancel;
    AutoLinearLayout centerPanel;
    ImageView fitnnn;
    EditText etNickName;
    EditText etPhoneNumber;
    ImageView fitnnnnn;
    EditText etVerifyNum;
    TextView tvGetVerify;
    ImageView fitnnnn;
    EditText etPass;
    Button btRegister;
    ImageView ivThirdQq;
    ImageView ivThirdWeibo;
    ImageView ivThirdWeixin;

    @Override
    protected int getLayout() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

        return R.layout.activity_register;
    }

    @Override
    protected void setData() {

    }

    @Override
    protected void init() {
        tvCancel = r2v(R.id.tv_cancel);
        tvCancel.setOnClickListener(this);
        centerPanel = r2v(R.id.center_panel);
        fitnnn = r2v(R.id.fitnnn);
        etNickName = r2v(R.id.et_nickName);
        etPhoneNumber = r2v(R.id.et_phoneNumber);
        fitnnnnn = r2v(R.id.fitnnnnn);
        tvGetVerify = r2v(R.id.tv_getVerify);
        tvGetVerify.setOnClickListener(this);
        fitnnnn = r2v(R.id.fitnnnn);
        etPass = r2v(R.id.et_pass);
        btRegister = r2v(R.id.bt_register);
        btRegister.setOnClickListener(this);
        ivThirdQq = r2v(R.id.iv_third_qq);
        ivThirdQq.setOnClickListener(this);
        ivThirdWeibo = r2v(R.id.iv_third_weibo);
        ivThirdWeibo.setOnClickListener(this);
        ivThirdWeixin = r2v(R.id.iv_third_weixin);
        etVerifyNum = r2v(R.id.et_verifyNum);

        ivThirdWeixin.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.tv_getVerify:
                OkHttpClientUtils.getDataAsync(this, URLConstants.USER.getVerifyCode + "/?phone=" + etPhoneNumber.getText().toString(), new RequestCallback() {
                    @Override
                    public void onFailure(Request request, Exception e) {
                        JUtils.Log("getDataAsync", "onfailure");
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(ResponceWrapper response) {
                        JUtils.Log("getDataAsync", response.getBody());
                    }

                    @Override
                    public void onStart() {

                    }
                }, "");

                break;
            case R.id.bt_register:
                if (!ableLogin()) return;

                String jsonObject = RequestParams.getNewInstance()
                        .setNickname(etNickName.getText().toString())
                        .setPhone(etPhoneNumber.getText().toString())
                        .setVerificationcode(etVerifyNum.getText().toString())
                        .setPassword(etPass.getText().toString())
                        .getJson().toString();

                OkHttpClientUtils.postJsonStringAsync(this, URLConstants.USER.Register, jsonObject, new RequestCallback() {
                    @Override
                    public void onFailure(Request request, Exception e) {
                        JUtils.Log("getDataAsync", "onFailure");
                    }

                    @Override
                    public void onResponse(ResponceWrapper response) {
                        JUtils.Log("getDataAsync", response.getBody());
                    }

                    @Override
                    public void onStart() {

                    }
                }, "");

                break;
            case R.id.iv_third_qq:
                break;
            case R.id.iv_third_weibo:
                break;
            case R.id.iv_third_weixin:
                break;
            case R.id.tv_cancel:
                finish();
                break;
        }
    }

    public boolean ableLogin() {
        if (TextUtils.isEmpty(etNickName.getText().toString())) {
            JUtils.Toast("请输入昵称");
            return false;
        }

        if (TextUtils.isEmpty(etPhoneNumber.getText().toString()) || !CommonUtils.isMobileNumber(etPhoneNumber.getText().toString())) {
            JUtils.Toast("手机号不合法");
            return false;
        }

        if (TextUtils.isEmpty(etVerifyNum.getText().toString())) {
            JUtils.Toast("请输入验证码");
            return false;
        }

        if (TextUtils.isEmpty(etPass.getText().toString())) {
            JUtils.Toast("请输入密码");
            return false;
        }

        return true;
    }

}
